package com.pang.security.auth;

import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pang.security.dto.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.RescaleOp;
import java.io.IOException;

/**
 * @version 1.0
 * 功能目的描述 ：用于   登录成功的处理类  ========>>>>>>>>>>>
 *
 * UsernamePasswordAuthenticationFilter 这个类中规定 请求一定要是 post类型 否则要报错
 * @author： pang-yun
 * @date： 2021-06-23 14:11
 */
@Component
public class MyAuthenticationSuccessHandler extends JSONAuthentication implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //以json格式返回一个提示
        //Result类的Json数据
        Result result= Result.ok().message("登录成功");
      /*  //处理编码情况，防止中文乱码
        response.setContentType("text/json;charset=utf-8");
        //塞到HttpServletResponse中返回给前台
        //这是springboot中jackson给我们提供的一种能将类或者其他对象转换成json的方式
        response.getWriter().write(new ObjectMapper().writeValueAsString(result));*/

        this.WriteJSON(request,response,result);
    }
}
